/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import DTO.TinTucDTO;
import com.sun.org.apache.bcel.internal.generic.BREAKPOINT;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import vietnamnet.ConvertVietnamnet;
import vnexpress.ConvertVnexpress;
/**
 *
 * @author Mr_Quay
 */
public class NewJFrame extends javax.swing.JFrame {

    Vector data = new Vector();
    Vector title = new Vector();

    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() throws SQLException, IOException, ClassNotFoundException {
        initComponents();
        LoadDS();
        setResizable(false);//không thay đổi Frame
        jComboBox1.setEnabled(false);
        cbbexpress.setEnabled(false);
    }

    public void Load(String link) throws SQLException, IOException, ClassNotFoundException {
        DefaultTableModel dm = (DefaultTableModel) jTable2.getModel();
        dm.setNumRows(0);
        List<TinTucDTO> lstDoiSong = DAO.TinTucDAO.layDSTinTuc(link);
        for (int i = 0; i < lstDoiSong.size(); i++) {
            TinTucDTO dto = lstDoiSong.get(i);
            Vector row = new Vector();
            row.add(dto.getTenweb());
            row.add(dto.getTieude());
            row.add(dto.getLink());
            row.add(dto.getNgaydang());
            row.add(dto.getHinhanh());
            row.add(dto.getTomtat());
            data.add(row);
        }
        jTable2.setModel(new DefaultTableModel(data, title));
    }

    public void LoadDS() throws SQLException, IOException, ClassNotFoundException {
        title.add("Tên Web");
        title.add("Tiêu đề");
        title.add("Link");
        title.add("Ngày đăng");
        title.add("Link Hình");
        title.add("Tóm tắt");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jbtXoa = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        lbImage = new javax.swing.JLabel();
        lbDate = new javax.swing.JLabel();
        lbTitle = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        taTomtat = new javax.swing.JTextArea();
        tfurl = new javax.swing.JTextField();
        btXpath = new javax.swing.JButton();
        btSave = new javax.swing.JButton();
        cbbexpress = new javax.swing.JComboBox();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Web", "Tiêu Đề", "Link", "Ngày Tháng", "Hình Ảnh", "Tóm tắt"
            }
        ));
        jTable2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable2MouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTable2);

        jLabel1.setText("URL:");

        jLabel2.setText("Chọn chuyên mục:");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--------Chọn---------", "Đời sống", "Xã hội", "Chính trị", "Giáo dục", "Khoa học", "Kinh tế", "Quốc tế", "CNTT-Viễn Thông" }));
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jbtXoa.setText("Xóa Tin");
        jbtXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtXoaActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder("Tin tức"))));

        taTomtat.setColumns(20);
        taTomtat.setRows(5);
        jScrollPane3.setViewportView(taTomtat);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(79, 79, 79)
                .addComponent(lbImage, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(42, 42, 42)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 419, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbDate, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 378, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(70, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbImage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lbDate, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lbTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        btXpath.setText("Lấy Xpath");
        btXpath.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btXpathActionPerformed(evt);
            }
        });

        btSave.setText("Save");

        cbbexpress.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--------Chọn--------", "Thời sự", "Thế giới", "Khoa học", "Pháp luật", "Tâm sự", "Ô tô-Xe máy", "Bạn đọc", "Cười" }));
        cbbexpress.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbbexpressActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jbtXoa)
                                .addGap(19, 19, 19))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(19, 19, 19)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(cbbexpress, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 774, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(310, 310, 310)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btSave)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfurl, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btXpath)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(106, 106, 106)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbbexpress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(97, 97, 97)
                        .addComponent(jbtXoa)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfurl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)
                            .addComponent(btXpath))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                        .addComponent(btSave)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowOpened

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
        // TODO add your handling code here:
        try {
            String temp = (String) this.jComboBox1.getSelectedItem();
            switch (temp) {
                case "Đời sống":
                    Load("http://vietnamnet.vn/vn/doi-song/");
                    break;
                case "Xã hội":
                    Load("http://vietnamnet.vn/vn/xa-hoi/");
                    break;
                case "CNTT-Viễn Thông":
                    Load("http://vietnamnet.vn/vn/cong-nghe-thong-tin-vien-thong/");
                    break;
                case "Chính trị":
                    Load("http://vietnamnet.vn/vn/chinh-tri/");
                    break;
                case "Giáo dục":
                    Load("http://vietnamnet.vn/vn/giao-duc/");
                    break;
                case "Khoa học":
                    Load("http://vietnamnet.vn/vn/khoa-hoc/");
                    break;
                case "Kinh tế":
                    Load("http://vietnamnet.vn/vn/kinh-te/");
                    break;
                case "Quốc tế":
                    Load("http://vietnamnet.vn/vn/quoc-te/");
                    break;
            }

        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, e);
        }
    }//GEN-LAST:event_jComboBox1ItemStateChanged

    private void jbtXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtXoaActionPerformed
        // TODO add your handling code here:
        try {
            int n = jTable2.getSelectedRow();
            String date = (String) jTable2.getValueAt(n, 3);
            int click = JOptionPane.showConfirmDialog(null, "Bạn có chắc muốn xóa Tin đã chọn không?", "Question", JOptionPane.YES_NO_OPTION);
            if (click == JOptionPane.YES_OPTION) {
                Connection con = null;
                try {
                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                    con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=TINTUC;user=han;password=123456");
                    Statement st = con.createStatement();
                    String strSQL = "DELETE FROM tintuc WHERE Date like '" + date + "'";
                    int rs = st.executeUpdate(strSQL);
                    JOptionPane.showMessageDialog(null, "Xóa thành công");
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "Thất Bại");
                    //e.printStackTrace();
                }
                NewJFrame nj = new NewJFrame();
                nj.setVisible(true);
                this.setVisible(false);

            }
            if (click == JOptionPane.NO_OPTION) {
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Chưa Chọn Tin Cần Xóa!");
        }
    }//GEN-LAST:event_jbtXoaActionPerformed

    private void jTable2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable2MouseClicked
        // TODO add your handling code here:
        int n = jTable2.getSelectedRow();
        String date = (String) jTable2.getValueAt(n, 3);
        String title = (String) jTable2.getValueAt(n, 1);
        String tomtat = (String) jTable2.getValueAt(n, 5);
        String image = (String) jTable2.getValueAt(n, 4);
        lbDate.setText(date);
        lbTitle.setText(title);
        taTomtat.setText(tomtat);
        try {
            BufferedImage img = ImageIO.read(new URL(image));
            ImageIcon a = new ImageIcon(img);
            Image imge = a.getImage();
            Image newimg = img.getScaledInstance(this.lbImage.getWidth(), this.lbImage.getHeight(), java.awt.Image.SCALE_SMOOTH);
            a = new ImageIcon(newimg);
            this.lbImage.setIcon(a);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, e);
        }
    }//GEN-LAST:event_jTable2MouseClicked

    private void cbbexpressActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbbexpressActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbbexpressActionPerformed

    private void btXpathActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btXpathActionPerformed
        // TODO add your handling code here:
        try{
            ConvertVietnamnet vn = new ConvertVietnamnet();
            ConvertVnexpress ex = new ConvertVnexpress();
            String url = tfurl.getText();
            switch (url) {
                case "http://vietnamnet.vn":
                    jComboBox1.setEnabled(true);
                    cbbexpress.setEnabled(false);
                    break;
                case "http://vnexpress.net":
                    cbbexpress.setEnabled(true);
                    jComboBox1.setEnabled(false);
                    break;
            }
        }catch(Exception e)
        {
            JOptionPane.showConfirmDialog(rootPane,e);
        }
    }//GEN-LAST:event_btXpathActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;




                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new NewJFrame().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btSave;
    private javax.swing.JButton btXpath;
    private javax.swing.JComboBox cbbexpress;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JButton jbtXoa;
    private javax.swing.JLabel lbDate;
    private javax.swing.JLabel lbImage;
    private javax.swing.JLabel lbTitle;
    private javax.swing.JTextArea taTomtat;
    private javax.swing.JTextField tfurl;
    // End of variables declaration//GEN-END:variables
}
